/*
 * @Author: your name
 * @Date: 2021-09-15 10:43:13
 * @LastEditTime: 2021-09-23 11:54:35
 * @LastEditors: zshawk1982
 * @Description: In User Settings Edit
 * @FilePath: \media_system\database\models\user.js
 */
var moment = require('moment');
const bcrypt = require('bcryptjs')
module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define('user', {
    userId: {
      type: DataTypes.UUID,
      primaryKey: true,
      defaultValue: DataTypes.UUIDV4,
      comment: '用户ID'
    },
    username: {
      type: DataTypes.STRING,
      comment: '用户名（昵称）',
      unique: true
    },
    realname:{
      type: DataTypes.STRING,
      comment: '真实姓名',
      defaultValue: "匿名"
    },
    password: {
      type: DataTypes.STRING,
      set(value) { 
        //bycrpt加密下载太费力,使用bycrptjs完成密码加密
        this.setDataValue('password', bcrypt.hashSync(value,5));
      },
      comment: '用户密码'
    },
    gender: {//（female 女  male男 other其他）
      type: DataTypes.STRING,
      comment: '性别'
    },
    height: {
      type: DataTypes.DOUBLE,
      comment: '身高'
    },
    weight: {
      type: DataTypes.DOUBLE,
      comment: '体重'
    },
    phone: {
      type: DataTypes.STRING,
      comment: '电话号码'
    },
    bmi: {
      type: DataTypes.DOUBLE,
      comment: 'bmi值'
    },
    specialCondition: {//（0正常 1 1型糖尿病 2 2型糖尿病 3混合型糖尿病  4其他特殊类型）
      type: DataTypes.INTEGER,
      comment: '糖尿病类型'
    },
    type: {//0 管理员 1 普通用户
      type: DataTypes.INTEGER,
      comment: '用户类型'
    },
    status: {//0 停用  1 正常
      type: DataTypes.INTEGER,
      comment: '用户状态'
    },
    birthDay:{
      type: DataTypes.STRING,
      comment: '出生日期'
    },
    totalKcal:{
      type: DataTypes.DOUBLE,
      comment: '每日总热量'
    },
    logoImg: {
      type: DataTypes.TEXT,
      comment: '用户头像'
    },
    createdAt: {
      type: DataTypes.DATE,
      get() {
        const createdAt = this.getDataValue('createdAt')
       return moment(createdAt).format('YYYY-MM-DD HH:mm:ss');
      },
      set(createdAt) {
        return this.setDataValue('createdAt',createdAt)
      }
    },
    updatedAt: {
      type: DataTypes.DATE,
      get() {
        const updatedAt = this.getDataValue('updatedAt')
        // createAt is an instance of date, you could operate it with moment library
        // to get ur format
       return moment(updatedAt).format('YYYY-MM-DD HH:mm:ss');
      },
      set(updatedAt) {
        return this.setDataValue('updatedAt',updatedAt)
      }
    }
  },
    {
      tableName: 'users',
      timestamps: true,
      // hooks: {
      //   afterBulkUpdate: (instance, options) => {
      //     console.log('findAll has been called on MyModel',instance);
      //   },
      // },
    });


  return User
}



